<template>
  <div>
    <!-- 三级联动 -->
    <el-card>
      <CategorySelector
        @changeCategory="changeCategory"
        :isShowList="isShowList"
      ></CategorySelector>
    </el-card>

    <!-- 下面的列表页 -->

    <el-card style="margin-top: 20px">
      <!-- 这个div是spu的列表的界面 -->
      <div v-show="!isShowSpuFrom && !isShowSkuFrom">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="showAddSpuFrom"
          :disabled="!category3Id"
          >添加SPU</el-button
        >

        <el-table :data="spuList" border style="width: 100%">
          <el-table-column type="index" align="center" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <HintButton
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="showAddSkuFrom(row)"
              ></HintButton>
              <HintButton
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="showUpdateSpuFrom(row)"
              ></HintButton>
              <HintButton
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看spu中的sku列表"
                @click="showSkuList(row)"
              >
              </HintButton>

              <el-popconfirm
                :title="`你确定要删除${row.spuName}吗？`"
                @onConfirm="deleteSpu(row)"
              >
                <HintButton
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                ></HintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <!--   
               -->
        <el-pagination
          background
          style="text-align: center"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
          layout="  prev, pager, next, jumper,->,sizes,total"
          :page-sizes="[2, 5, 10]"
          :current-page="page"
          :page-size="limit"
          :pager-count="5"
          :total="total"
        >
        </el-pagination>
      </div>

      <!-- 这个div是spu的添加或者修改界面 -->
      <!-- <SpuFrom  v-show="isShowSpuFrom"   :isShowSpuFrom="isShowSpuFrom"  @update:isShowSpuFrom="isShowSpuFrom=$event"></SpuFrom>          -->
      <SpuFrom
        ref="spu"
        v-show="isShowSpuFrom"
        :isShowSpuFrom.sync="isShowSpuFrom"
        @successBack="successBack"
      ></SpuFrom>

      <!-- 添加sku的界面 -->
      <SkuFrom
        ref="sku"
        v-show="isShowSkuFrom"
        :isShowSkuFrom.sync="isShowSkuFrom"
      ></SkuFrom>
    </el-card>

    <!-- 点击查看spu中sku的列表的dialog框-->
    <el-dialog
        :before-close="closeDialog"
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
    >
      <el-table   :data="skuList"   v-loading="loading"  >
        <el-table-column
          property="skuName"
          label="名称"
          width="150"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="200"
        ></el-table-column>
        <el-table-column property="weight" label="重量"></el-table-column>
        <el-table-column  label="默认图片">
              <template slot-scope="{row,$index}">
                   <img :src="row.skuDefaultImg" alt="x" style="width:100px;height:100px">
              </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuFrom from "@/views/product/Spu/components/SkuFrom.vue";
import SpuFrom from "@/views/product/Spu/components/SpuFrom.vue";
export default {
  name: "Spu",
  components: { SkuFrom, SpuFrom },
  data() {
    return {
      isShowList: true, //这里这个数据传递过去只是为了解决可操作性，就是三级联动禁用或者不禁用的
      category1Id: "", //一级分类id
      category2Id: "", //二级分类id
      category3Id: "", //三级分类id
      attrList: [], //属性数据列表

      //定义分页器相关初始化数据，后面请求来的数据可以直接覆盖
      page: 1,
      limit: 2,
      total: 0,

      spuList: [], //定义一个数组接收请求回来的spu数据

      //下面这两个数据是控制三个页面之间的切换
      isShowSpuFrom: false, //是否显示SpuFrom组件
      isShowSkuFrom: false, //是否显示SkuFrom组件

      //下面数据是查看spu中sku的列表
      dialogTableVisible: false, //dialog框开关
      spu: {},
      skuList: [], //渲染dialog的列表，请求来的数据
       loading: false
    };
  },
  watch: {
    isShowSpuFrom: {
      handler(newValue, oldValue) {
        this.isShowList = !newValue;
      },
    },
    isShowSkuFrom: {
      handler(newValue, oldValue) {
        this.isShowList = !newValue;
      },
    },
  },
  methods: {
    //这个函数的目的是可以正常使用三级联动
    changeCategory({ categoryId, level }) {
      if (level === 1) {
        (this.category2Id = ""), (this.category3Id = "");
        (this.attrList = []), (this.category1Id = categoryId);
      } else if (level === 2) {
        this.category3Id = "";
        (this.attrList = []), (this.category2Id = categoryId);
      } else {
        this.category3Id = categoryId;
        this.getSpuList(); //定义函数后面发请求获取spu数据
      }
    },

    //发请求获取spu的分页列表
    async getSpuList(pager = 1) {
      this.page = pager;
      try {
        const result = await this.$API.spu.getList(
          this.page,
          this.limit,
          this.category3Id
        );
        if (result.code === 200 || result.code === 20000) {
          this.spuList = result.data.records;
          this.total = result.data.total;
        } else {
          this.$message.error("获取spu列表失败");
        }
      } catch (error) {
        this.$message.error("请求获取spu列表失败");
      }
    },
    //切换每页数量
    handleSizeChange(size) {
      this.limit = size;
      this.getSpuList();
    },

    //点击添加spu按钮，显示SpuFrom组件
    showAddSpuFrom() {
      this.isShowSpuFrom = true;
      //点击'添加'按钮，要发后面两个请求
      this.$refs.spu.getInitAddSpuFormData(this.category3Id);
    },

    //点击修改spu按钮
    showUpdateSpuFrom(row) {
      this.isShowSpuFrom = true;

      //点击这个修改spu按钮时候，调用子组件中的一个方法，让子组件中的发请求函数能触发
      this.$refs.spu.getInitUpdateSpuFormData(row, this.category3Id);
    },

    //点击添加sku的按钮
    showAddSkuFrom(row) {
      this.isShowSkuFrom = true;

      //隔山打牛，点击'添加sku'按钮，调用SkuFrom中的发请求函数获取初始化数据
      this.$refs.sku.getInitAddSkuFromData(
        row,
        this.category1Id,
        this.category2Id,
        this.category3Id
      );
    },

    //SpuFrom组件'保存'成功的回调
    successBack(spuId) {
      if (spuId) {
        //修改成功回来的
        this.getSpuList(this.page);
      } else {
        //添加成功回来的
        this.getSpuList();
      }
    },

    //点击删除spu
    async deleteSpu(row) {
      try {
        const result = await this.$API.spu.remove(row.id);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("删除spu成功");
          this.getSpuList(
            this.getSpuList.length > 1 ? this.page : this.page - 1
          );
        } else {
          this.$message.error("删除spu失败");
        }
      } catch (error) {
        this.$message.error("请求删除spu失败");
      }
    },

    //点击“查看spu中的sku列表”
    async showSkuList(row) {
      this.dialogTableVisible = true;
      this.spu = row; //上面需要用到spu的名字
        //打开dialog的时候清空之前的数据
        // this.skuList = []
      this.loading = true
      //发请求获取sku的列表
      try {
        const result = await this.$API.sku.getListBySpuId(this.spu.id);
        if(result.code === 200 || result.code === 20000){
            this.$message.success('成功')
             this.skuList = result.data
        }else{
            this.$message.error('获取sku列表失败')
        }
      } catch (error) {
          this.$message.error('请求获取sku列表失败')
      }
        this.loading = false
    },
    //dialog关闭之前的回调
    closeDialog(done){
         this.skuList = []
        done()
        // this.dialogTableVisible = false//这个跟done都可以关闭
        this.loading = false
    }
  },
};
</script>

<style>
</style>